import React, { useState, useEffect } from "react";
import MainLayout from "@component/mainLayout/mainLayout";
import LivenessCon from "../components/livenessCon/index";
import { connect } from "react-redux";
import { checkBonus } from "@redux/Action/bank/bank";
import { Toast } from "antd-mobile";
import creditStore from "../creditStore";
import xhfTool from "./xhfTool";

const LivenessPage = props => {
	useEffect(() => {
		console.log("【人脸】props:", props);
		let infoStr = sessionStorage.getItem("credit_faceParams_info");
		console.log(infoStr);
		props._checkBonus(`${props.currentCode}0${props.current}`);
	}, []);

	const startLive = async () => {
		const {
			authHeaderStr,
			lnid,
			applyNo,
			devicesInfo
		} = props.getAppPlatformsDatas;
		let infoStr = sessionStorage.getItem("credit_faceParams_info") || "";
		let { userName, idNo } = infoStr ? JSON.parse(infoStr) : {};
		let post_data = {
			authHeaderStr,
			lnid,
			applyNo,
			devicesInfo,
			userName,
			idNo
		};
		let resp = await xhfTool.startLiveFace(post_data);
		if (resp.status === "1000" && resp.data.code === "0") {
			const openStatus = resp.data.data.applyStatus || "";
			const failMessage = resp.data.message || "";
			props.goNext(openStatus, failMessage);
		} else {
			Toast.info(resp.data.message, 5);
		}
	};

	//查看人脸协议
	const checkFaceAgreement = e => {
		e.stopPropagation();
		props.history.push("/faceAgreement");
	};

	const pageParams = {
		header: {
			title: "人脸识别"
		},
		isModal: true,
		history: props.history,
		cacheLifecycles: props.cacheLifecycles
	};

	return (
		<div>
			<MainLayout pageParams={pageParams} handleBack={props.handleBack}>
				<LivenessCon
					onClick={startLive}
					checkFaceAgreement={checkFaceAgreement}
				></LivenessCon>
			</MainLayout>
		</div>
	);
};

const mapStateToProps = state => state;
const mapDispatchToProps = dispatch => {
	return {
		_checkBonus: (val1, val2) => dispatch(checkBonus(val1, val2))
	};
};

export default connect(
	mapStateToProps,
	mapDispatchToProps
)(creditStore(LivenessPage));
